package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_sj.domain.ReportSjbhwxnsyBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjbhwxnsyBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjBhwxnsyjcjlBsInfoBo;
import com.ruoyi.report_sj.domain.vo.ReportSjBhwxnsyjcjlBsInfoVo;
import com.ruoyi.report_sj.domain.ReportSjBhwxnsyjcjlBsInfo;
import com.ruoyi.report_sj.mapper.ReportSjBhwxnsyjcjlBsInfoMapper;
import com.ruoyi.report_sj.service.IReportSjBhwxnsyjcjlBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆拌合物性能试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjBhwxnsyjcjlBsInfoServiceImpl implements IReportSjBhwxnsyjcjlBsInfoService {

    private final ReportSjBhwxnsyjcjlBsInfoMapper baseMapper;
    private final ReportSjbhwxnsyBaDetailMapper detailMapper;

    /**
     * 查询砂浆拌合物性能试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSjBhwxnsyjcjlBsInfoVo queryById(Long excelId) {
        ReportSjBhwxnsyjcjlBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSjbhwxnsyBaDetail> detailList = detailMapper.selectListByExcelId(excelId);
        if (detailList == null || detailList.size() == 0) {
            for (int i = 0; i < 2; i++) {
                ReportSjbhwxnsyBaDetail detail = new ReportSjbhwxnsyBaDetail();
                detail.setExcelId(excelId);
                detailMapper.insert(detail);
                detailList.add(detail);
            }
        }
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询砂浆拌合物性能试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSjBhwxnsyjcjlBsInfoVo> queryPageList(ReportSjBhwxnsyjcjlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjBhwxnsyjcjlBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSjBhwxnsyjcjlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆拌合物性能试验检测记录列表
     */
    @Override
    public List<ReportSjBhwxnsyjcjlBsInfoVo> queryList(ReportSjBhwxnsyjcjlBsInfoBo bo) {
        LambdaQueryWrapper<ReportSjBhwxnsyjcjlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjBhwxnsyjcjlBsInfo> buildQueryWrapper(ReportSjBhwxnsyjcjlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjBhwxnsyjcjlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSjBhwxnsyjcjlBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSjBhwxnsyjcjlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSjBhwxnsyjcjlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSjBhwxnsyjcjlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSjBhwxnsyjcjlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSjBhwxnsyjcjlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSjBhwxnsyjcjlBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSjBhwxnsyjcjlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSjBhwxnsyjcjlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSjBhwxnsyjcjlBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSjBhwxnsyjcjlBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyrq() != null, ReportSjBhwxnsyjcjlBsInfo::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSjBhwxnsyjcjlBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSjBhwxnsyjcjlBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSjBhwxnsyjcjlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSjBhwxnsyjcjlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSjBhwxnsyjcjlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShajzl()), ReportSjBhwxnsyjcjlBsInfo::getShajzl, bo.getShajzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSjBhwxnsyjcjlBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(bo.getBiaogmdcz() != null, ReportSjBhwxnsyjcjlBsInfo::getBiaogmdcz, bo.getBiaogmdcz());
        lqw.eq(bo.getFencd() != null, ReportSjBhwxnsyjcjlBsInfo::getFencd, bo.getFencd());
        lqw.eq(bo.getXiacsd() != null, ReportSjBhwxnsyjcjlBsInfo::getXiacsd, bo.getXiacsd());
        lqw.eq(bo.getBaosl() != null, ReportSjBhwxnsyjcjlBsInfo::getBaosl, bo.getBaosl());
        lqw.eq(bo.getPingjhsl() != null, ReportSjBhwxnsyjcjlBsInfo::getPingjhsl, bo.getPingjhsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSjBhwxnsyjcjlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSjBhwxnsyjcjlBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSjBhwxnsyjcjlBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSjBhwxnsyjcjlBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSjBhwxnsyjcjlBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增砂浆拌合物性能试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportSjBhwxnsyjcjlBsInfoBo bo) {
        ReportSjBhwxnsyjcjlBsInfo add = BeanUtil.toBean(bo, ReportSjBhwxnsyjcjlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改砂浆拌合物性能试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportSjBhwxnsyjcjlBsInfoBo bo) {
        ReportSjBhwxnsyjcjlBsInfo update = BeanUtil.toBean(bo, ReportSjBhwxnsyjcjlBsInfo.class);
        validEntityBeforeSave(update);
        //更新子表
        List<ReportSjbhwxnsyBaDetail> detailList = bo.getDetailList();
        if (detailList != null) {
            detailList.forEach(vo -> vo.setExcelId(bo.getExcelId()));
            detailMapper.deleteByExcelId(bo.getExcelId());
        }
        detailMapper.insertBatch(detailList);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjBhwxnsyjcjlBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆拌合物性能试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
